package com.bootdo.app.controller;

import com.bootdo.app.service.AppSellerService;
import com.bootdo.app.utils.AppUtilXML;
import com.bootdo.app.utils.ZimeBankUtil;
import com.bootdo.app.utils.returnresponse.ServerResponse;
import com.bootdo.appsystem.domain.ImageinfoDO;
import com.bootdo.appsystem.domain.ProducesinfoDO;
import com.bootdo.appsystem.domain.SellerDO;
import com.bootdo.appsystem.domain.UseraccountDO;
import com.bootdo.appsystem.service.ImageinfoService;
import com.bootdo.appsystem.service.ProducesinfoService;
import com.bootdo.appsystem.service.SellerService;
import com.bootdo.common.controller.BaseController;
import com.bootdo.common.domain.FileDO;
import org.omg.CORBA.ServerRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 商品接口
 */
@Controller
@RequestMapping("/app/goods/")
public class AppGoodsController extends BaseController {

    @Autowired
    private SellerService sellerService;
    @Autowired
    private AppSellerService appSellerService;
    @Autowired
    private ImageinfoService imageinfoService;
    @Autowired
    private ProducesinfoService producesinfoService;


    //添加商品页面
    @GetMapping("toaddgoods")
    public String toAddGoods (){
        return "app/seller/addMail";
    }

    /**
     * 上传商家门头图片
     * @param file
     * @param request
     * @return
     */
    @ResponseBody
    @PostMapping("addGoodsZhutu")
    public ServerResponse msgAddGoodsZhutu(@RequestParam("file")MultipartFile file , HttpServletRequest request , String addGoodsCode , Integer imageIndex , Integer imageType){
        if(file == null){
            return ServerResponse.createByErrorMessage("未上传图片");
        }
        UseraccountDO useraccountDO = (UseraccountDO)request.getSession().getAttribute(AppUtilXML.SESSION_CUSTOMER);

        if(addGoodsCode == null || addGoodsCode.equals("0")){
            addGoodsCode = ZimeBankUtil.getACode();
        }
        FileDO sysFile = appSellerService.uploadSellerImage(file,useraccountDO);

        ImageinfoDO img = new ImageinfoDO();
        img.setAllid(Integer.parseInt(addGoodsCode));
        img.setImgdesc("商品图片");
        img.setType(imageType);
        img.setImagetype(0);
        img.setDisplay(imageIndex);
        img.setImgurl(sysFile.getUrl());

        if(imageType.equals(12008)){
            HashMap<String,Object> map = new HashMap<String,Object>();
            map.put("type",12008);
            map.put("allid",Integer.parseInt(addGoodsCode));
            List<ImageinfoDO> list = imageinfoService.list(map);
            if (list.size() > 0){
                ImageinfoDO imageinfoDO = list.get(0);
                imageinfoDO.setImgurl(sysFile.getUrl());
                imageinfoService.update(imageinfoDO);
            } else {
                imageinfoService.save(img);
            }
        }else {
            imageinfoService.save(img);
        }
        return ServerResponse.createBySuccess(addGoodsCode,sysFile);
    }

    /**
     * 保存商品信息
     * @param product
     * @param addGoodsCode
     * @return
     */
    @ResponseBody
    @PostMapping("msgsavegoods")
    public ServerResponse msgSaveGoods(ProducesinfoDO product , String addGoodsCode){
        UseraccountDO useraccountDO = (UseraccountDO)this.getRequest().getSession().getAttribute(AppUtilXML.SESSION_CUSTOMER);
        SellerDO seller = sellerService.getSellerByUserId(useraccountDO.getUid());
        product.setSellerid(seller.getSellerid());
        product.setIsbest(1);
        product.setIshot(1);
        product.setIsnew(1);
        product.setIsfree(1);
        product.setSells(0);
        product.setBestcount(0);
        product.setMediumcount(0);
        product.setBadcount(0);
        product.setPtime(new Date());
        HashMap<String,Object> map = new HashMap<>();
        map.put("allid",Integer.parseInt(addGoodsCode));
        map.put("type",12008);
        ImageinfoDO imageinfoDO = imageinfoService.list(map).get(0);
        product.setFavorable(imageinfoDO.getImgurl());
        int save = producesinfoService.save(product);
        imageinfoService.updateGoodsAllid(Integer.parseInt(addGoodsCode),product.getPid());
        return ServerResponse.createBySuccess();
    }


    //    购买商品页面
    @GetMapping("buygoods/{pid}")
    public String toSellerInfoAndImage(@PathVariable("pid")Integer pid , Model model){
        ProducesinfoDO producesinfoDO = producesinfoService.get(pid);
        if(producesinfoDO == null){
            return "app/404";
        }
        model.addAttribute("producesinfoDO" , producesinfoDO);
        return "app/order/orderTo";
    }





}
